#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent, IcySettings *fSettings,
                       IcyBuddies *fBuddies) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setupMenus();
    setupWindow();
    tabbedWidget = 0;
    settings = fSettings;
    buddies = fBuddies;

    /// Temporary
    xStatusWidget *xStatus = new xStatusWidget(0, settings);
    xStatus->show();

    IcyBuddy *buddy = new IcyBuddy(settings);
    IcyBuddy *buddy2 = new IcyBuddy(settings);
    IcyBuddy *group = new IcyBuddy(settings);
    IcyBuddy *group2 = new IcyBuddy(settings);

    buddy->setAlias("RankoR");
    buddy->setGroup(1);
    buddy->setType(1);
    buddy->setIndex(-1);
    buddy->setScreenName("210091");
    buddy->setStatus(0);
    buddy->setXStatus(0);

    buddy2->setType(1);
    buddy2->setGroup(2);
    buddy2->setAlias("Miracle");

    group->setAlias("General");
    group->setType(0);
    group->setGroup(1);

    group2->setAlias("Friends");
    group2->setType(0);
    group2->setGroup(2);
    buddies->append(group);
    buddies->append(group2);
    buddies->append(buddy);
    buddies->append(buddy2);

    /// end of temporary
    refreshBuddies();
}

MainWindow::~MainWindow()
{
    trayIcon->hide();
    delete ui;
    if ( tabbedWidget != 0 )
        delete tabbedWidget;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

// Refreshs UI Buddies list
void MainWindow::refreshBuddies()
{
    for ( qint32 i = 0; i < treeItems.count(); i++ )
        delete (treeItems.at(i));

    treeItems.clear();

    IcyBuddy buddy(settings), buddy3(settings);
    qint32 iCount = buddies->count();
    QList<QTreeWidgetItem *> treeGroupItems;
    for ( qint32 i = 0; i < iCount; i++ ){
        buddy = *(buddies->at(i));

        if ( buddy.type() != 0 )
            continue;

        treeGroupItems.append(new QTreeWidgetItem((QTreeWidget*) 0,
                              QStringList(buddy.alias())));

        for ( qint32 s = 0; s < iCount; s++ ){
            buddy3 = *(buddies->at(s));
            if ( buddy3.type() == 0 || buddy3.group() != buddy.group() )
                continue;

            treeItems.append(new QTreeWidgetItem(treeGroupItems.at(treeGroupItems.count()-1),
                             QStringList(buddy3.alias())));
            treeItems.at(treeItems.count()-1)->setIcon(0, settings->getIcon("status://offline"));
        }
    }
    iCount = treeGroupItems.count();
    for ( qint32 i = 0; i < iCount; i++ ){
        treeItems.append(treeGroupItems.at(i));
    }
    ui->treeWidget->insertTopLevelItems(0, treeItems);
}

// Settings up Main Widget
void MainWindow::setupWindow()
{
    setWindowIcon(settings->getIcon("status://offline"));
    setWindowTitle("");
    trayIcon = new QSystemTrayIcon(settings->getIcon("status://offline"), this);
    trayIcon->setContextMenu(mainMenu);
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(onTrayClicked(QSystemTrayIcon::ActivationReason)));
    trayIcon->show();
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::WindowCloseButtonHint);
    settingsWidget = 0;
    searchWidget = 0;

}

// Setting up menues
void MainWindow::setupMenus()
{
    ui->optionsBtn->setText(tr("Menu"));
    ui->optionsBtn->setIcon(settings->getIcon("system://options"));
    ui->statusBtn->setText(tr("Status"));
    ui->statusBtn->setIcon(settings->getIcon("status://offline"));
    mainMenu = new QMenu((QWidget*) ui->optionsBtn);
    statusMenu = new QMenu((QWidget*) ui->statusBtn);
    pStatusMenu = new QMenu(tr("Status"), (QWidget*) statusMenu);
    xStatusMenu = new QMenu(tr("XStatus"), (QWidget*) statusMenu);
    contactMenu = new QMenu(ui->treeWidget);

    // Contact Menu

    actionOpenChat = contactMenu->addAction(settings->getIcon("system://message"),
                                            tr("Send message"));

    // Main Menu

    actionOptions = mainMenu->addAction(settings->getIcon("options"), tr("Options"));
    actionInfo = mainMenu->addAction(settings->getIcon("info"), tr("Info"));
    actionSearch = mainMenu->addAction(settings->getIcon("search"), tr("Search"));
    actionAbout = mainMenu->addAction(settings->getIcon("about"), tr("About"));
    actionClose = mainMenu->addAction(settings->getIcon("Close"), tr("Close"));

    // Status Menu
    actionStatusOffline = pStatusMenu->addAction(settings->getIcon("status://offline"),
                                                 tr("Offline"));
    actionStatusOnline = pStatusMenu->addAction(settings->getIcon("status://online"),
                                                 tr("Online"));
    actionStatusDND = pStatusMenu->addAction(settings->getIcon("status://dnd"),
                                                 tr("DND"));
    actionStatusBusy = pStatusMenu->addAction(settings->getIcon("status://occupied"),
                                                 tr("Occupied"));
    actionStatusAway = pStatusMenu->addAction(settings->getIcon("status://away"),
                                                 tr("Away"));
    actionStatusAngry = pStatusMenu->addAction(settings->getIcon("status://evil"),
                                                 tr("Evil"));
    actionStatusDepression = pStatusMenu->addAction(settings->getIcon("status://depression"),
                                                 tr("Depression"));
    actionStatusFFC = pStatusMenu->addAction(settings->getIcon("status://ffc"),
                                                 tr("Free For Chat"));

    // Sig-Slots
    connect(actionOptions, SIGNAL(triggered()), this, SLOT(onSettingsClicked()));
    connect(actionSearch, SIGNAL(triggered()), this, SLOT(onSearchClicked()));
    connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));
    connect(actionOpenChat, SIGNAL(triggered()), this, SLOT(onOpenChatClicked()));
    connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)),
            this, SLOT(onContextMenuRequested(QPoint)));

    //Status Menu

    statusMenu->addMenu(pStatusMenu);
    statusMenu->addSeparator();
    statusMenu->addMenu(xStatusMenu);

    mainMenu->setVisible(true);
    mainMenu->setEnabled(true);

    ui->optionsBtn->setMenu(mainMenu);
    ui->statusBtn->setMenu(statusMenu);
    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
}

void MainWindow::setAccount(const IcyAccount &fAccount)
{
    account = new IcyAccount;
    *account = fAccount;
}


/************************************************************************
            SLOTS
************************************************************************/

// Tree Widget Context Menu called
void MainWindow::onContextMenuRequested(QPoint point)
{
    contactMenu->setGeometry(x() + ui->treeWidget->x() + point.x(),
                             y() + ui->treeWidget->y() + point.y() +
                             contactMenu->height(),
                             contactMenu->width(), contactMenu->height());
    contactMenu->show();
}

// Settings action
void MainWindow::onSettingsClicked()
{
    if ( settingsWidget == 0 )
        settingsWidget = new SettingsWidget;

    settingsWidget->setVisible(true);
}

// Search action
void MainWindow::onSearchClicked()
{
    if ( searchWidget == 0 )
        searchWidget = new SearchWidget;

    searchWidget->setVisible(true);
}

// Open Chat action
void MainWindow::onOpenChatClicked()
{
    if ( tabbedWidget == 0 )
        tabbedWidget = new ChatTabbedWidget(0, buddies);

    tabbedWidget->addWidget("210091", "RankoR");
}

// CloseBtn clicked
void MainWindow::on_closeBtn_clicked()
{
    hide();
}

void MainWindow::onTrayClicked(QSystemTrayIcon::ActivationReason reason)
{
    switch ( reason ){
        case QSystemTrayIcon::Trigger:
            if ( isVisible() )
                hide();
            else
                show();
            break;

        case QSystemTrayIcon::DoubleClick:
            break;

        default:
            trayIcon->contextMenu()->show();
            break;
    }
}
